package com.uchengwang.ui.cm_ui.umsg.http;

import android.app.Activity;
import android.text.TextUtils;

import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.listener.IHttpManager;
import com.uchengwang.functions.http.listener.OnHttpResultListener;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.ui.cm_ui.umsg.bean.EnjoyChatBean;
import com.uchengwang.ui.cm_ui.umsg.bean.EnjoyChatListBean;
import com.uchengwang.ui.cm_ui.umsg.bean.EnjoyChatNoticeBean;
import com.uchengwang.ui.cm_ui.umsg.bean.EnjoyChatNoticeListBean;
import com.uchengwang.ui.utils.JsonUtils;
import com.uchengwang.ui.utils.LoadingUtils;

/**
 * Created by U城网 on 2018/3/22.
 * 享聊新的api
 */

public class EnjoyChatManager {
    private static EnjoyChatManager manager;

    private EnjoyChatManager (){}
    public static EnjoyChatManager getManager() {
        if (manager ==null){
            manager = new EnjoyChatManager();
        }
        return manager;
    }

    /**
     * 发布享聊
     * */
    public void releaseChat(Activity activity, MapRequest map, final OnResultListener listener){
        LoadingUtils.getUtils().loading(activity);
        HttpManager.newInstance(activity).query(IHttpManager.RequestMethod.POST, "/chat", map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }
        });
    }

    /**
     * 删除享聊
     * */
    public void deleteChat(Activity activity, long emotionId, final OnResultListener listener){
        LoadingUtils.getUtils().loading(activity);
        MapRequest map = new MapRequest();
        map.put(BaseConfig.EMOTION_ID, emotionId);
        HttpManager.newInstance(activity).query(IHttpManager.RequestMethod.DELETE, "/chat", map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }
        });
    }

    /**
     * 获取享聊
     * */
    public void getChats(Activity activity, MapRequest map, final OnHttpResultListener<EnjoyChatBean> listener){
        HttpManager.newInstance(activity).query(IHttpManager.RequestMethod.GET, "/chat", map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                if (status ==0 && !TextUtils.isEmpty(data) && !data.equals("[]")){
                    EnjoyChatListBean bean = JsonUtils.getManager().stringToBean(data, EnjoyChatListBean.class);
                    listener.onResult(status, msg, bean.getTotalPage(), bean.getData());
                }else {
                    listener.onResult(status, msg, 0, null);
                }
            }
        });
    }


    /**
     * 获取享聊提示
     * */
    public void getChatNotice(Activity activity, MapRequest map, final OnHttpResultListener<EnjoyChatNoticeBean> listener){
        LoadingUtils.getUtils().loading(activity);
        HttpManager.newInstance(activity).query(IHttpManager.RequestMethod.GET, "/chat/notice", map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                LoadingUtils.getUtils().hide();
                if (status ==0 && !TextUtils.isEmpty(data) && !data.equals("[]")){
                    EnjoyChatNoticeListBean bean = JsonUtils.getManager().stringToBean(data, EnjoyChatNoticeListBean.class);
                    listener.onResult(status, msg, bean.getTotalPage(), bean.getData());
                }else {
                    listener.onResult(status, msg, 0, null);
                }
            }
        });
    }
}
